﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using System.Windows.Controls;

namespace Xceed.Silverlight.DataGrid
{
  internal static class LayoutOrientationExtensions
  {
    internal static LayoutRange GetOrientedRange( this Rect rect, Orientation orientation )
    {
      return ( orientation == Orientation.Vertical )
        ? new LayoutRange( rect.Y, rect.Height )
        : new LayoutRange( rect.X, rect.Width );
    }

    internal static double GetOrientedPosition( this Rect rect, Orientation orientation )
    {
      return ( orientation == Orientation.Vertical )
        ? rect.Y
        : rect.X;
    }

    internal static Rect SetOrientedPosition( this Rect rect, Orientation orientation, double value )
    {
      if( orientation == Orientation.Vertical )
      {
        rect.Y = value;
      }
      else
      {
        rect.X = value;
      }

      return rect;
    }

    internal static double GetOrientedLength( this Rect rect, Orientation orientation )
    {
      return ( orientation == Orientation.Vertical )
        ? rect.Height
        : rect.Width;
    }

    internal static Rect SetOrientedLength( this Rect rect, double length, Orientation orientation )
    {
      if( orientation == Orientation.Vertical )
      {
        rect.Height = length;
      }
      else
      {
        rect.Width = length;
      }

      return rect;
    }

    internal static Size SetOrientedLength( this Size size, double length, Orientation orientation )
    {
      if( orientation == Orientation.Vertical )
      {
        size.Height = length;
      }
      else
      {
        size.Width = length;
      }

      return size;
    }

    internal static double GetOrientedLength( this Size size, Orientation orientation )
    {
      return ( orientation == Orientation.Vertical )
        ? size.Height
        : size.Width;
    }

    internal static double GetOrientedLength( this IAnimatedLayoutElement element, Orientation orientation )
    {
      return ( orientation == Orientation.Vertical )
        ? element.Height
        : element.Width;
    }

    internal static void SetOrientedLength( this IAnimatedLayoutElement element, double length, Orientation orientation )
    {
      if( orientation == Orientation.Vertical )
      {
        element.Height = length;
      }
      else
      {
        element.Width = length;
      }
    }

    internal static void SetOrientedAnimationPosition( this IAnimatedLayoutElement element, double position, Orientation orientation )
    {
      if( orientation == Orientation.Vertical )
      {
        element.AnimationY = position;
      }
      else
      {
        element.AnimationX = position;
      }
    }

    internal static Size Convert( this Size size, Orientation initialOrientation )
    {
      if( initialOrientation == Orientation.Horizontal )
      {
        return new Size( size.Height, size.Width );
      }
      return size;
    }

    internal static Size ConvertBack( this Size size, Orientation initialOrientation )
    {
      return size.Convert( initialOrientation );
    }

    internal static LayoutRange Convert( this LayoutRange range, FetchDirection direction, double layoutDirectionLength )
    {
      if( direction == Xceed.Silverlight.DataGrid.FetchDirection.Backward )
      {
        range = new LayoutRange( layoutDirectionLength - ( range.Offset + range.Length ), range.Length );
      }

      return range;
    }

    internal static LayoutRange ConvertBack( this LayoutRange range, FetchDirection direction, double layoutDirectionLength )
    {
      return range.Convert( direction, layoutDirectionLength );
    }

    internal static Rect Convert( this Rect rect, Orientation initialOrientation )
    {
      return rect
        .FlipRectOrientation( initialOrientation );
    }

    internal static Rect ConvertBack( this Rect rect, Orientation initialOrientation )
    {
      return rect.FlipRectOrientation( initialOrientation );
    }

    internal static Rect FlipRectOrientation( this Rect rect, Orientation initialOrientation )
    {
      if( initialOrientation == Orientation.Horizontal )
      {
        rect = new Rect( rect.Y, rect.X, rect.Height, rect.Width );
      }

      return rect;
    }

    internal static Rect AddPosition( this Rect rect, Point position )
    {
      return new Rect( rect.X + position.X, rect.Y + position.Y, rect.Width, rect.Height );
    }

    internal static Point GetPosition( this Rect rect )
    {
      return new Point( rect.X, rect.Y );
    }

    internal static Rect SetPosition( this Rect rect, Point position )
    {
      return new Rect( position, rect.GetSize() );
    }

    internal static Size GetSize( this Rect rect )
    {
      return new Size( rect.Width, rect.Height );
    }
  }
}
